package com.niuxiaofa.newlaw.Activity;


import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.azhon.appupdate.dialog.NumberProgressBar;
import com.azhon.appupdate.listener.OnDownloadListenerAdapter;
import com.azhon.appupdate.manager.DownloadManager;
import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.UserInfoBean;
import com.niuxiaofa.newlaw.Fragment.EnergyFragment;
import com.niuxiaofa.newlaw.Fragment.LawUniversalFragment;
import com.niuxiaofa.newlaw.Fragment.MallFragment;
import com.niuxiaofa.newlaw.Fragment.MeFragment;
import com.niuxiaofa.newlaw.Fragment.WisdomLibraryFragment;
import com.niuxiaofa.newlaw.Network.UserPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.Constant;
import com.niuxiaofa.newlaw.Tools.GetLog;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.adapter.CustomViewPager;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.jzvd.Jzvd;
import io.reactivex.functions.Consumer;

/**
 * @author Gxjkz
 * @describe
 * @date 2020-8-14 14:29:15
 * @Version 1.0
 */
public class MainActivity extends BaseActivity implements UserPresenter.interface_userInfo {

    @BindView(R.id.customViewPager)
    CustomViewPager customViewPager;


    private FragmentPagerAdapter fragmentPagerAdapter;

    private WisdomLibraryFragment wisdomLibraryFragment;
    private EnergyFragment energyfragment;
    private LawUniversalFragment lawUniversalFragment;
    private MallFragment mallFragment;
    private MeFragment meFragment;

    private List<Fragment> mFragments = new ArrayList<>();


     @BindView(R.id.number_progress_bar)
    NumberProgressBar progressBar;

    private DownloadManager manager;


    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar mBottomNavigationBar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }


    private void requestPermisson() {
        RxPermissions rxPermission = new RxPermissions(this);
        rxPermission
                .request(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,//存储权限
                        Manifest.permission.READ_EXTERNAL_STORAGE
                )
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {

                        } else {
//                            SetPermissionDialog mSetPermissionDialog = new SetPermissionDialog(MainActivity.this);
//                            mSetPermissionDialog.show();
//                            mSetPermissionDialog.setConfirmCancelListener(new SetPermissionDialog.OnConfirmCancelClickListener() {
//                                @Override
//                                public void onLeftClick() {
//                                    Toast("拒绝权限会导致部分功能不能使用");
//                                }
//
//                                @Override
//                                public void onRightClick() {
//
//
//                                }
//                            });
                        }
                    }
                });
    }


    @Override
    protected void initData() {
        requestPermisson();
        setSystemInvadeBlack(true);

        //   String sdkver = TXLiveBase.getSDKVersionStr();





        initViewPager();

        initializeBottomNavigationBar();
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        Constant.TOKEN = sharedPreferences.getString("token", "");
        GetLog.getLog(Constant.TOKEN);
        UserPresenter.userInfo(this);

        SharedPreferences share = UIUtils.getActivity().getSharedPreferences("protocolDialog", Context.MODE_PRIVATE);


//            AppTipDialog appTipDialog = new AppTipDialog(UIUtils.getActivity(), new AppTipDialog.Onclick() {
//                @Override
//                public void click() {
//
//                    SharedPreferences.Editor editor = share.edit();
//                    editor.putLong("apptip", UUDateUtils.getUnixTimestamp());
//                    editor.commit();
//
//
//                }
//            });
//
//            appTipDialog.show();


        //   UserPresenter.newVersion(this); //检测新版本


    }


    private void initViewPager() {


        wisdomLibraryFragment = new WisdomLibraryFragment();
        energyfragment = new EnergyFragment();
        lawUniversalFragment = new LawUniversalFragment();
        mallFragment = new MallFragment();
        meFragment = new MeFragment();
        mFragments.add(wisdomLibraryFragment);
        mFragments.add(energyfragment);
        mFragments.add(lawUniversalFragment);
        mFragments.add(mallFragment);
        mFragments.add(meFragment);


        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {

                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };


        customViewPager.setOffscreenPageLimit(5);
        customViewPager.setScanScroll(false);
        customViewPager.setAdapter(fragmentPagerAdapter);
        customViewPager.setCurrentItem(2);
    }

    private void initializeBottomNavigationBar() {
        // TODO 设置模式
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        // TODO 设置背景色样式
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mBottomNavigationBar.setBarBackgroundColor(R.color.white);

        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.wisdomlibrary_on, "智库").setActiveColorResource(R.color.black)
                        .setInactiveIconResource(R.mipmap.energy_off).setInActiveColorResource(R.color.listitem_gray))
                .addItem(new BottomNavigationItem(R.mipmap.energy_on, "能量").setActiveColorResource(R.color.black)
                        .setInactiveIconResource(R.mipmap.energy_off).setInActiveColorResource(R.color.listitem_gray))
                .addItem(new BottomNavigationItem(R.mipmap.lawuniversal_on, "普法").setActiveColorResource(R.color.black)
                        .setInactiveIconResource(R.mipmap.lawuniversal_off).setInActiveColorResource(R.color.listitem_gray))
                .addItem(new BottomNavigationItem(R.mipmap.mall_on, "商城").setActiveColorResource(R.color.black)
                        .setInactiveIconResource(R.mipmap.mall_off).setInActiveColorResource(R.color.listitem_gray))
                .addItem(new BottomNavigationItem(R.mipmap.my_on, "我的").setActiveColorResource(R.color.black)
                        .setInactiveIconResource(R.mipmap.my_off).setInActiveColorResource(R.color.listitem_gray))
                .setFirstSelectedPosition(0)
                .initialise();
        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {


                if (position == 1) {
                    energyfragment.startplayvideo();
                } else {
                    Jzvd.releaseAllVideos();
                }
                customViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
        mBottomNavigationBar.selectTab(2);

    }


    private OnDownloadListenerAdapter listenerAdapter = new OnDownloadListenerAdapter() {
        /**
         * 下载中
         *
         * @param max      总进度
         * @param progress 当前进度
         */
        @Override
        public void downloading(int max, int progress) {
            int curr = (int) (progress / (double) max * 100.0);
            progressBar.setMax(100);
            progressBar.setProgress(curr);
        }
    };

//    @Override
//    public void getVersion(NewVersionBean u) {
//        if (u.getStatus() == 0) {
//            /*
//             * 整个库允许配置的内容
//             * 非必选
//             */
//
//
//
//           int v1= getvison(u.getData().getVersion());
//
//            int v2= getvison(SystemUtil.getAppVersionName(UIUtils.getActivity()));
//
//
//
//            if (v1>v2) {
//                UpdateConfiguration configuration = new UpdateConfiguration()
//                        //输出错误日志
//                        .setEnableLog(false)
//                        //设置自定义的下载
//                        //.setHttpManager()
//                        //下载完成自动跳动安装页面
//                        .setJumpInstallPage(true)
//                        //设置对话框背景图片 (图片规范参照demo中的示例图)
//                     //   .setDialogImage(R.mipmap.ic_dialog)
//                        //设置按钮的颜色
//                        .setDialogButtonColor(Color.parseColor("#FD913D"))
//                        //设置对话框强制更新时进度条和文字的颜色
//                        //.setDialogProgressBarColor(Color.parseColor("#E743DA"))
//                        //设置按钮的文字颜色
//                        .setDialogButtonTextColor(Color.WHITE)
//                        //设置是否显示通知栏进度
//                        .setShowNotification(true)
//                        //设置是否提示后台下载toast
//                        .setShowBgdToast(true)
//                        //设置强制更新
//                  //      .setForcedUpgrade(u.getData().getIsForce() == 1 ? true : false)
//                        //设置对话框按钮的点击监听
//                        .setButtonClickListener(new OnButtonClickListener() {
//                            @Override
//                            public void onButtonClick(int id) {
//
//                            }
//                        })
//                        //设置下载过程的监听
//                        .setOnDownloadListener(listenerAdapter);
//
//                manager = DownloadManager.getInstance(this);
//                manager.setApkName("lingzhang.apk")
//                 //       .setApkUrl(u.getData().getAndroidUrl())
//                        .setSmallIcon(R.mipmap.ic_launcher)
//                        .setShowNewerToast(true)
//                        .setConfiguration(configuration)
//                        .setApkVersionCode(1000)
//                //        .setApkVersionName(u.getData().getVersion())
//                        //      .setApkSize("20.4")
//                //        .setApkDescription(u.getData().getUpdateDesc())
////                .setApkMD5("DC501F04BBAA458C9DC33008EFED5E7F")
//                        .download();
//            }
//        }
//    }

    public int getvison(String version) {

        String[] version_no = version.split("\\.");

        int vsr1 = Integer.parseInt(version_no[0]) * 100;
        int vsr2 = Integer.parseInt(version_no[1]) * 10;
        int vsr3 = Integer.parseInt(version_no[2]);

        return vsr1 + vsr2 + vsr3;

    }


    @Override
    public void getResponse(UserInfoBean u) {
        if (u.getCode() == 1) {
            Constant.ISLOGIN = true;
//            lawUniversalFragment.setIs_login(true);
//        } else {
//            lawUniversalFragment.setIs_login(false);
        }
    }
}
